$(function() {

	$("#loadingmodel").modal({
		backdrop : 'static',
		keyboard : 'false',
		show : "true"
	});

	console.log("请求地址：" + baseUrl + premiseselurl);
	//查询所有的房产信息内容，并放置在jsonArray里面
	var premiseselData = {
		kParam : []
	};
	var data = {

	};
	premiseselData.kParam.push(data);
	var jsonArray = [];
	var viewModel;
	$.ajax({
		type : "POST",
		url : baseUrl + premiseselurl,
		timeout : 20000, //超时时间设置，单位毫秒
		data : JSON.stringify(premiseselData),
		contentType : "text/plain",
		dataType : "json",
		success : function(args) {
			$('#loadingmodel').modal('hide');
			var sResultData = args.resultData;
			var sResultMsg = args.resultMsg;
			var sResultCode = args.resultCode;
			console.log("success");
			if (sResultCode == 0) {
				for (var i = 0; i < sResultData.length; i++) {
					var jsonObject = {
						"name" : sResultData[i].name,
						"url" : sResultData[i].url,
						"remarks" : sResultData[i].remarks,
					}
					jsonArray.push(jsonObject);
				}
				console.log(jsonArray);
			}
			viewModel = new ViewModel(jsonArray);
			ko.applyBindings(viewModel);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$('#loadingmodel').modal('hide');
			console.log("error");
		}
	});
	$('#normalbutton').click(function() {

	});

	//Knockout绑定
	var RowModel = function(obj) {
		var self = this;
		if ( typeof obj !== "object")
			obj = {};
		self.name = ko.observable(obj.name || "");
		self.url = ko.observable(obj.url || "0");
		self.remarks = ko.observable(obj.remarks || "￥");
	};
	var ViewModel = function(jsonArray) {
		var self = this;
		var data = [];
		jsonArray.forEach(function(item) {
			data.push(new RowModel(item));
		});
		self.data = ko.observableArray(data);
		/*self.agree = function(item) {
		 var agreeNum = item.agreeNum();
		 var disagreeNum = item.disagreeNum();
		 var declare = item.hasDeclare();
		 if (declare == "") {
		 item.agreeNum(agreeNum + 1);
		 } else if (declare == "disagree") {
		 item.agreeNum(agreeNum + 1);
		 item.disagreeNum(disagreeNum - 1);
		 }
		 item.hasDeclare("agree");
		 };
		 self.disagree = function(item) {
		 var agreeNum = item.agreeNum();
		 var disagreeNum = item.disagreeNum();
		 var declare = item.hasDeclare();
		 if (declare == "") {
		 item.disagreeNum(disagreeNum + 1);
		 } else if (declare == "agree") {
		 item.disagreeNum(disagreeNum + 1);
		 item.agreeNum(agreeNum - 1);
		 }
		 item.hasDeclare("disagree");
		 }*/
	};

	/*var jsonArray = [{
	"name" : "华新 山水居",
	"url" : "../../img/fc1.png",
	"remarks" : '<p>华新地产，登峰之作、环山路正席，最后宝地、城央正脉，咫尺繁华、一山双水双公园，泰城唯一、顶级圈层，纯粹高端社区、低密科技大宅，大师级作品！</p>',
	}];*/

	//构造控件实例
	var listview = UM.listview("#listview");

	//添加控件方法
	/*listview.on("pullDown", function(sender) {
	//这是可以编写列表下拉加载逻辑，参数sender即为当前列表实例对象
	var row = {
	"head": "../../img/head2.jpg",
	"name": "来自星星的你",
	"ifVip": false,
	"source": "开心网",
	"url": "../../img/adurl4.png",
	"remarks": "球拍刚拿到手，比想象的好，物美价廉，五星级推荐！！！",
	"agreeNum": 60,
	"disagreeNum": 55,
	"comments": 40
	};
	viewModel.data.unshift(new RowModel(row));
	sender.refresh();
	});*/
	/*listview.on("pullUp", function(sender) {
	//这是可以编写列表上拉刷新逻辑，参数sender即为当前列表实例对象
	var row = {
	"head": "../../img/head2.jpg",
	"name": "华新",
	"ifVip": true,
	"source": "网易",
	"url": "../../img/fc2.png",
	"remarks" : '<p>华新地产，登峰之作、环山路正席，最后宝地、城央正脉，咫尺繁华、一山双水双公园，泰城唯一、顶级圈层，纯粹高端社区、低密科技大宅，大师级作品！</p>',
	"agreeNum": 60,
	"disagreeNum": 55,
	"comments": 40
	};
	viewModel.data.push(new RowModel(row));
	jsonArray.push(row);
	sender.refresh();
	});*/
	//点击后展示房产信息的更多详情。
	listview.on("itemClick", function(sender, args) {
		//这里可以处理行点击事件，参数sender即为当前列表实例对象，args对象有2个属性，即rowIndex(行索引)和$target(目标行的jquery对象)
		//alert("您点击了列表第"+ (args.rowIndex+1) + "行！");
		$('#fcDetail .um-content').empty();
		$('#fcDetail .um-content').scrollTop(0);
		$('#fcxx .um-empty').click();
		var newsItem = jsonArray[args.rowIndex];
		var $div = $(' <div class="news-container"> <h3>' + newsItem.name + '</h3> <div class="news-head f12"> <div class="news-time fl" ></div> <div class="news-comment fr"><span></span></div> </div> <img src="' + newsItem.url + '" /> <div class="news-content" >' + newsItem.remarks + '</div></div>');
		$('#fcDetail .um-content').append($div);
	});
}); 